package io.lkt.modules.sys.controller;

import com.google.common.collect.Lists;
import io.lkt.common.utils.PageUtils;
import io.lkt.common.utils.R;
import io.lkt.common.validator.ValidatorUtils;
import io.lkt.modules.sys.entity.BArticleInfoEntity;
import io.lkt.modules.sys.entity.BArticleStandEntity;
import io.lkt.modules.sys.entity.BArticleTypeInfoEntity;
import io.lkt.modules.sys.service.BArticleInfoService;
import io.lkt.modules.sys.service.BArticleStandService;
import io.lkt.modules.sys.service.BArticleTypeInfoService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 待推送文章
 *
 * @author xss
 * @email dustwas@163.com
 * @date 2021-02-03 11:22:52
 */
@RestController
@RequestMapping("/sys/barticlestand")
public class BArticleStandController {
    @Autowired
    private BArticleStandService bArticleStandService;

    @Autowired
    private BArticleInfoService bArticleInfoService;
    @Autowired
    private BArticleTypeInfoService bArticleTypeInfoService;


    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("sys:barticlestand:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = bArticleStandService.queryPage(params);
        List<?> list = page.getList();

        if (CollectionUtils.isNotEmpty(list)) {
            List<BArticleStandEntity> bArticleStandEntityList = Lists.newArrayList();

            for (Object temp : list) {
                BArticleStandEntity bArticleStand = (BArticleStandEntity) temp;

                BArticleInfoEntity bArticleInfo = this.bArticleInfoService.getById(bArticleStand.getArticleId());
                if (bArticleInfo != null && bArticleInfo.getArticleTitle() != null) {
                    bArticleStand.setArticleTitle(bArticleInfo.getArticleTitle());
                }

                BArticleTypeInfoEntity bArticleTypeInfo = bArticleTypeInfoService.getById(bArticleStand.getArticleTypeId());
                if (bArticleTypeInfo != null && bArticleTypeInfo.getArticleTypeName() != null) {
                    bArticleStand.setArticleTypeName(bArticleTypeInfo.getArticleTypeName());
                }

                bArticleStandEntityList.add(bArticleStand);
            }
            page.setList(bArticleStandEntityList);
        }

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("sys:barticlestand:info")
    public R info(@PathVariable("id") Integer id) {
        BArticleStandEntity bArticleStand = bArticleStandService.getById(id);

        BArticleInfoEntity bArticleInfo = this.bArticleInfoService.getById(bArticleStand.getArticleId());
        bArticleStand.setArticleTitle(bArticleInfo.getArticleTitle());

        BArticleTypeInfoEntity bArticleTypeInfo = bArticleTypeInfoService.getById(bArticleStand.getArticleTypeId());
        bArticleStand.setArticleTypeName(bArticleTypeInfo.getArticleTypeName());

        return R.ok().put("bArticleStand", bArticleStand);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("sys:barticlestand:save")
    public R save(@RequestBody BArticleStandEntity bArticleStand) {
        bArticleStandService.save(bArticleStand);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("sys:barticlestand:update")
    public R update(@RequestBody BArticleStandEntity bArticleStand) {
        ValidatorUtils.validateEntity(bArticleStand);

        bArticleStand.setCreateTime(new Date());
        bArticleStandService.updateById(bArticleStand);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("sys:barticlestand:delete")
    public R delete(@RequestBody Integer[] ids) {
        bArticleStandService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 保存
     */
    @RequestMapping("/savePush")
//    @RequiresPermissions("sys:barticlestand:save")
    public R savePush(@RequestBody BArticleInfoEntity bArticleInfo) {

        if (bArticleInfo == null || bArticleInfo.getId() == null || bArticleInfo.getResourceType() == null) {
            return R.error("参数异常");
        }

        if (bArticleInfo.getArticleTypeId() == null) {
            return R.error("不是分类文章");
        }

        BArticleStandEntity bArticleStand = new BArticleStandEntity();
        bArticleStand.setArticleId(bArticleInfo.getId());
        bArticleStand.setArticleTypeId(bArticleInfo.getArticleTypeId());
        bArticleStand.setResourceType(bArticleInfo.getResourceType());

        bArticleStand.setStartTime(bArticleInfo.getTimeStart());
        bArticleStand.setEndTime(bArticleInfo.getTimeEnd());
        bArticleStand.setCreateTime(new Date());

        bArticleStandService.save(bArticleStand);

        return R.ok();
    }

}
